package com.su.pg.ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Toast;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.UiSettings;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.PolygonOptions;
import com.amap.api.maps2d.model.PolylineOptions;
import com.su.pg.R;
import com.su.pg.data.LatLngUtil;
import com.su.pg.data.PolygonData;

/**
 * Created by Choco on 2015/6/1.
 */
public class PGMainActivity extends Activity {
    private MapView mapView;
    private AMap aMap;
    private UiSettings uiSettings;
    private PolygonData polygonData;
    private PolylineOptions polylineOptions;
    private LatLng startPoint;
    private boolean createGeofencePhase = true;

    public PGMainActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.basicmap_activity);
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
        init();

        polygonData = new PolygonData();
    }

    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();

            uiSettings = aMap.getUiSettings();

            // 设置定位图标
            aMap.setLocationSource(new LocationSource() {
                @Override
                public void activate(OnLocationChangedListener onLocationChangedListener) {

                }

                @Override
                public void deactivate() {

                }
            });
            uiSettings.setMyLocationButtonEnabled(true);
            aMap.setMyLocationEnabled(true);

            // 指南针
            uiSettings.setCompassEnabled(true);

            // 比例尺
            uiSettings.setScaleControlsEnabled(true);

            aMap.setOnMapClickListener(new AMap.OnMapClickListener() {
                @Override
                public void onMapClick(LatLng latLng) {
                    Toast.makeText(PGMainActivity.this,
                            "标记点：(" + latLng.longitude + ", " + latLng.latitude + ")",
                            Toast.LENGTH_SHORT).show();

                    if(createGeofencePhase) {
                        if (polylineOptions == null) {
                            polylineOptions = new PolylineOptions();
                            polylineOptions.width(1).geodesic(true).color(Color.argb(255, 1, 1, 1));

                            startPoint = latLng;

                            // 添加一个点，暂不绘制边
                            polylineOptions.add(latLng);
                            polygonData.add(latLng);

                        } else {
                            if (LatLngUtil.isSameLatLng(startPoint, latLng, aMap.getScalePerPixel())) {
                                polylineOptions.add(startPoint);
                                aMap.addPolyline(polylineOptions);

                                // 标记结束
                                Toast.makeText(PGMainActivity.this,
                                        "地理围栏范围已确定",
                                        Toast.LENGTH_SHORT).show();


                                aMap.clear();

                                createGeofencePhase = false;

                                aMap.addPolygon(new PolygonOptions()
                                        .addAll(polygonData.get())
                                        .fillColor(Color.argb(0xCF, 0x23, 0xA4, 0x56)).strokeColor(Color.RED).strokeWidth(1));
                            } else {
                                polylineOptions.add(latLng);
                                aMap.addPolyline(polylineOptions);

                                polygonData.add(latLng);
                            }
                        }
                    } else{
                        if(LatLngUtil.pointInPolygon(polygonData.get(), latLng)){
                            Toast.makeText(PGMainActivity.this,
                                    "In range",
                                    Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(PGMainActivity.this,
                                    "Out of range",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
}